package com.xmtlxx.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.system.domain.bo.SysAreaBo;
import com.xmtlxx.system.domain.vo.SysAreaVo;
import com.xmtlxx.system.service.ISysAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 行政区划管理Controller
 *
 * @author xmtlxx
 * @date 2022-04-28
 */
@Validated
@Api(value = "行政区划管理控制器", tags = {"行政区划管理管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/sysArea")
public class SysAreaController extends BaseController {

    private final ISysAreaService iSysAreaService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<SysAreaVo> initBean(){
        SysAreaVo vo = new SysAreaVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询行政区划管理列表
     */
    @ApiOperation("查询行政区划管理下拉树列表")
//    @SaCheckPermission("system:sysArea:list")
    @GetMapping("/treeselect")
    public R<List<Tree<Long>>> treeselect(SysAreaBo bo) {
        List<SysAreaVo> list = iSysAreaService.queryList(bo);
        return R.ok(iSysAreaService.buildTreeSelect(list));
    }

    /**
     * 查询行政区划管理列表
     */
    @ApiOperation("查询行政区划管理列表")
//    @SaCheckPermission("system:sysArea:list")
    @GetMapping("/list")
        public R<List<SysAreaVo>> list(SysAreaBo bo) {
            if(bo.getLevels() == null && bo.getParentId() == null){
               bo.setLevels(1l);
            }
        return R.ok(iSysAreaService.queryList(bo));
    }

    /**
     * 导出行政区划管理列表
     */
    @ApiOperation("导出行政区划管理列表")
    @SaCheckPermission("system:sysArea:export")
    @Log(title = "行政区划管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated SysAreaBo bo, HttpServletResponse response) {
        List<SysAreaVo> list = iSysAreaService.queryList(bo);
        ExcelUtil.exportExcel(list, "行政区划管理", SysAreaVo.class, response);
    }

    /**
     * 获取行政区划管理详细信息
     */
    @ApiOperation("获取行政区划管理详细信息")
    @SaCheckPermission("system:sysArea:query")
    @GetMapping("/{areaId}")
    public R<SysAreaVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("areaId") String areaId) {
        return R.ok(iSysAreaService.queryById(areaId));
    }

    /**
     * 新增行政区划管理
     */
    @ApiOperation("新增行政区划管理")
    @SaCheckPermission("system:sysArea:add")
    @Log(title = "行政区划管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysAreaVo vo) {
        SysAreaBo bo = new SysAreaBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysAreaService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改行政区划管理
     */
    @ApiOperation("修改行政区划管理")
    @SaCheckPermission("system:sysArea:edit")
    @Log(title = "行政区划管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysAreaVo vo) {
        SysAreaBo bo = new SysAreaBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysAreaService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除行政区划管理
     */
    @ApiOperation("删除行政区划管理")
    @SaCheckPermission("system:sysArea:remove")
    @Log(title = "行政区划管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{areaIds}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] areaIds) {
        return toAjax(iSysAreaService.deleteWithValidByIds(Arrays.asList(areaIds), true) ? 1 : 0);
    }
}
